package com.czk.track;

import net.bytebuddy.asm.Advice;

import java.util.UUID;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/10 9:49
 */
public class MyAdvice {
    @Advice.OnMethodEnter
    public static void enter(@Advice.Origin("#t") String className, @Advice.Origin("#m") String methodName) {
        String linkId = TrackManager.getCurrentSpan();
        if (linkId == null) {
            linkId = UUID.randomUUID().toString().replaceAll("-", "");
            TrackContext.setLinkedId(linkId);
        }
        String entrySpan = TrackManager.createEntrySpan();
        System.out.println("链路追踪：" + entrySpan + " " + className + "." + methodName);
    }

    @Advice.OnMethodExit()
    public static void exit(@Advice.Origin("#t") String className, @Advice.Origin("#m") String methodName) {
        TrackManager.getExitSpan();
    }
}
